package com.wehealth.biz_ecg.helper;

import static com.wehealth.three.lib_connect.utils.EcgFileUtil.saveByteArrayToFile;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.wehealth.biz_ecg.ui.ECGMeasureActivity;
import com.wehealth.three.lib_connect.constant.CommonKeyConstant;
import com.wehealth.three.lib_connect.utils.AppGlobals;
import com.wehealth.three.lib_connect.utils.ByteDataUtil;
import com.wehealth.three.lib_connect.utils.StringUtils;

import java.io.File;
import java.util.LinkedList;

public class SaveFile24HThread extends Thread {
    private final int SAVE_PDF_XML_FILE_SUCCESS = 897;
    //
    public boolean saveFileThreadFLAG;
    private Handler handler;
    private long ecg2Device_time;

    public LinkedList<Integer[]> queue = new LinkedList<Integer[]>();

    public SaveFile24HThread(Handler mHandler, long ecg2DeviceData_time) {
        saveFileThreadFLAG = true;
        ecg2Device_time = ecg2DeviceData_time;
        handler = mHandler;
    }

    public void addToQueue(Integer[] dataInts) {
        synchronized (queue) {
            queue.add(dataInts);
        }
    }

    @Override
    public void run() {
        super.run();
        try {

            while (saveFileThreadFLAG) {
                if (queue.isEmpty()) {
                    Log.e("TAG", "ecg24h: sleep(5s)");
                    sleep(5 * 1000);
                } else {
                    synchronized (queue) {
                        Log.e("TAG", "ecg24h:  取一次数据");
                        Integer[] lists = queue.removeFirst();
                        save24EcgData(ecg2Device_time, lists, handler);
                        handler.sendEmptyMessageDelayed(SAVE_PDF_XML_FILE_SUCCESS, 100);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 设置为false，线程循环标识为false
     * @param flag
     */
    public void setThreadFlag(boolean flag) {
        saveFileThreadFLAG = flag;
    }

    /***
     * 保存24小时的心电数据
     * @param ecg2Device_time
     * @param dataArray
     * @param handler
     */
    private void save24EcgData(long ecg2Device_time, Integer[] dataArray, Handler handler) {
        if (ecg2Device_time < 0) {
            return;
        }
        try {
            String path;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                path = AppGlobals.INSTANCE.get().getExternalFilesDir(CommonKeyConstant.Version_DATA2_24H) + File.separator;
            } else {
                path = StringUtils.getSDPath() + "/ECGDATA/Data2Device/Hours24/";
                File fileDir = new File(path);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
            }
            int count = ECGMeasureActivity.measureActivity.saveManualMinuteCount;

            count = count - 1;
            int c = count / 60;
            path = path + ecg2Device_time + "_" + c + ".txt";
            File file24h = new File(path);
            saveByteArrayToFile(file24h.getAbsolutePath(), ByteDataUtil.toByteArray(dataArray));
        } catch (Exception e) {
            Message message = Message.obtain();
            message.obj = e.getMessage().toString();
            message.what = 11111000;
            handler.sendMessage(message);
            e.printStackTrace();
        }
    }


}
